/**
 * @File SegmentDaoImpl.java
 * @author Nupur Dixit
 * @Version 1.0
 * @Date April 26, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.generalLedger.segment;

import in.ac.dei.edrp.generalLedger.assetRegister.AssetBean;
import in.ac.dei.edrp.generalLedger.utilities.CommonFunctions;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import com.ibatis.sqlmap.client.SqlMapClient;

@Component
@Qualifier("SegmentDaoImpl")
public class SegmentDaoImpl extends SqlMapClientDaoSupport implements SegmentDao{
	
	private static Logger logObj = Logger.getLogger(SegmentDaoImpl.class);
	@Autowired
	private TransactionTemplate transactionTemplate;
	public SegmentDaoImpl() {}
	
	@Autowired
	public SegmentDaoImpl(SqlMapClient sqlMapClient){
		setSqlMapClient(sqlMapClient);
	}
	
	/*@Autowired
	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}*/
	
	/*@Autowired
	public SegmentDaoImpl(SqlMapClient sqlMapClient,TransactionTemplate transactionTemplate){
		setSqlMapClient(sqlMapClient);
		sett
	}*/

	public List<SegmentInfoBean> getColumns(SegmentInfoBean input) {
		List<SegmentInfoBean> columnList = new ArrayList<SegmentInfoBean>();
		try{
			columnList = getSqlMapClientTemplate().queryForList("segment.getColumn", input);
		}catch(Exception ex){
			System.out.println(ex);
			logObj.error("Exception inside getColumn Method of SegmentDaoImpl class :",ex);
		}
		return columnList;
	}

	public List<SegmentInfoBean> getValueSet(SegmentInfoBean input) {
		List<SegmentInfoBean> valueSetList = new ArrayList<SegmentInfoBean>();
		try{
			valueSetList = getSqlMapClientTemplate().queryForList("segment.getValueSet", input);
		}catch(Exception ex){
			System.out.println(ex);
			logObj.error("Exception inside getValueSet Method of SegmentDaoImpl class :",ex);
		}
		return valueSetList;
	}
	
	@SuppressWarnings("unchecked")
	public String setSegment(final SegmentInfoBean input) {		
		String s = (String) transactionTemplate.execute(new TransactionCallback() {
			public String doInTransaction(TransactionStatus tStatus) {												
				Object savepoint = null;
				String status="false";				
				try {
					savepoint = tStatus.createSavepoint();
					CommonFunctions common = new CommonFunctions(getSqlMapClient());
					String finalCode = common.generateNumericIds(input.getUniversityCode(), "FLXFID");
					System.out.println("final code: "+finalCode);
					common.updateNumericSystemId(input.getUniversityCode(), "FLXFID", finalCode, "add", true);					
					status="true-"+finalCode;
					input.setFlexFieldId(finalCode);
					getSqlMapClientTemplate().insert("segment.insertSegmentDetail", input);					
					}catch (Exception ex) {
						logger.error("Exception inside setSegment Method of SegmentDaoImpl class :",ex);
						status = "false-"+ex.getMessage();					
						tStatus.rollbackToSavepoint(savepoint);
						return status;
					}
				return status;
			}			
		});
		return s;
	}
}
